package com.example.danangtravel.Restaurant;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.exam.danangtranel.filed.Hotel;
import com.example.danangtravel.DataAccess;
import com.example.danangtravel.R;
import com.example.danangtravel.checknetwork;
import com.example.danangtravel.map.RoutePath;

public class Actitivy_Details extends Activity {
	Hotel itemClickOnListView;
	DataAccess dbAccess;
	TextView tvName;
	TextView tvAddress;
	TextView tvInformation;
	String images;
	// Gallery gallery;
	ImageButton map_res, btn_hDHotel;
	List<Bitmap> bitmaps;
	private checknetwork check;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.restaurant_actitivy_details);
		btn_hDHotel = (ImageButton) findViewById(R.id.btn_hDHotel);
		btn_hDHotel.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				finish();
			}
		});

		tvName = (TextView) findViewById(R.id.textView_restaurant_actitivy_details_name);
		tvAddress = (TextView) findViewById(R.id.textView_restaurant_actitivy_details_address);
		tvInformation = (TextView) findViewById(R.id.textView_restaurant_actitivy_details_Information);
		Bundle extras = getIntent().getExtras();
		// Intent myIntent = getIntent();
		// String idOfTable = extras.getString("idoftableFoodies");
		int id = extras.getInt("idoftableFoodies");
		// int id = Integer.parseInt(idOfTable);
		// int type = Integer.valueOf(myIntent.getStringExtra("type")) ;
		// Toast.makeText(getApplicationContext(), "id la:" + id, 1000).show();
		// String id=myIntent.getI

		try {
			dbAccess = new DataAccess(getApplicationContext());
			itemClickOnListView = new Hotel();

			itemClickOnListView = dbAccess.getAllFood1(id);

			tvName.setText("" + itemClickOnListView.getAdrress());
			tvAddress.setText("" + itemClickOnListView.getName());
			tvInformation.setText("" + itemClickOnListView.getInformation());
			Gallery gallery = (Gallery) findViewById(R.id.gallery_restaurant_actitivy_details_images);
			gallery.setSpacing(2);
			Bitmap bitmap = null;

			bitmaps = new ArrayList<Bitmap>();
			// xu ly de~ dua anh len gallery
			images = itemClickOnListView.getImage();
			// Toast.makeText(getApplicationContext(), "Chuoi images: "+images,
			// 1000).show();
			// tach chuoi thanh 1 mang chuoi []
			String[] imagesArray = images.split(" ");
			// // chay vong for de~ lay tung` phan` tu~ cua mang chuoi
			for (int i = 0; i < imagesArray.length; i++) {
				// truyen vao` ham` de~ lay' hinh` tu` asset va` chuyen thanh`
				// bitmap
				bitmap = this.getBitmapFromAsset("food/drink/"
						+ imagesArray[i].toString());
				// gan' vao` mang List<Bitmap>
				bitmaps.add(i, bitmap);

				// Toast.makeText(getApplicationContext(),
				// "Chuoi images: "+imagesArray[i], 1000).show();
			}

			// goi adapter cua gallery
			ImagesAdapterGallery adapterGallery = new ImagesAdapterGallery(
					getApplicationContext(), bitmaps);
			// hien len gallery
			gallery.setAdapter(adapterGallery);
			gallery.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub

					final Dialog settingsDialog = new Dialog(
							Actitivy_Details.this);
					settingsDialog.getWindow().requestFeature(
							Window.FEATURE_NO_TITLE);
					settingsDialog.setContentView(R.layout.image_dialog);
					Bitmap bitmat = Bitmap.createScaledBitmap(
							bitmaps.get(arg2), 350, 350, true);
					ImageView image = (ImageView) settingsDialog
							.findViewById(R.id.gallerytest);
					image.setImageBitmap(bitmat);

					image.setOnClickListener(new OnClickListener() {

						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							settingsDialog.cancel();
						}
					});
					settingsDialog.show();
				}
			});

			dbAccess.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map_res = (ImageButton) findViewById(R.id.btnMapRes);
		map_res.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				check = new checknetwork(v.getContext());

				String lar = itemClickOnListView.getLongitude();
				String lon = itemClickOnListView.getLatitude();
				if (check.isOnline()) {
					Intent i = new Intent(v.getContext(), RoutePath.class);
					i.putExtra("latitude", lar);
					i.putExtra("longitude", lon);
					v.getContext().startActivity(i);
				} else
					Toast.makeText(v.getContext(), "Không có kết nối !", 1000)
							.show();
			}
		});

	}

	// chuyen may hinh` cua~ 1 doi tuong thanh` may' bitmap cua 1 doi tuong
	// public List<Bitmap_Images> traveBitmap() {
	// // String imagesString=travels.get().imagesString;
	// List<Bitmap_Images> listIdBitmap=new ArrayList<Bitmap_Images>();
	// Bitmap bit=null;
	//
	// for (int i = 0; i < imagesOfItem.size(); i++) {
	// Bitmap_Images idbitmap=new Bitmap_Images();
	// String imagesString = imagesOfItem.get(i);
	// String images[] = imagesString.split(" ");
	// for (int j = 0; j < images.length; j++) {
	// bit=this.getBitmapFromAsset("image_travel/"+images[j].toString());
	// idbitmap.addBitmap(bit);
	// }
	// listIdBitmap.add(idbitmap);
	// }
	// return listIdBitmap;
	// }

	private Bitmap getBitmapFromAsset(String strName) {
		AssetManager assetManager = getApplicationContext().getAssets();
		InputStream istr = null;
		try {
			istr = assetManager.open(strName);

		} catch (IOException e) {
			e.printStackTrace();
		}
		Bitmap bitmap = BitmapFactory.decodeStream(istr);
		return bitmap;
	}

}
